<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class service extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_service');
        $this->load->model('m_bagian');
        $this->load->model('m_lokasi');
        $this->load->model('m_barang');
        $this->load->model('m_group_barang');
        $this->load->model('m_kategoribarang');
        $this->load->model('m_jenisbarang');
        $this->load->model('m_merk');
        $this->load->model('m_pengajuan_barang');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        $this->load->library('datetimemanipulation');
        $this->load->library('phpexcel');

        // lightbox
        $this->smarty->load_javascript("resource/js/lightbox/lightbox-2.6.min.js");
        $this->smarty->load_style("lightbox/css/lightbox.css");
        // autonumeric
        $this->smarty->load_javascript("resource/js/autonumeric/autonumeric.js");

        //select 2
        $this->smarty->load_javascript("resource/js/select2/select2.js");
        $this->smarty->load_style("select2/select2.css");
    }

    public function index() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/service/list.html");

        //cari
        $search = $this->tsession->userdata('search_service');
        $barang_noinventaris = empty($search['barang_noinventaris']) ? '%%' : "%".$search['barang_noinventaris']."%";
        $barang_nama = empty($search['barang_nama']) ? '%%' : "%".$search['barang_nama']."%";
        $tahun = empty($search['tahun']) ? '%%' : "%".$search['tahun']."%";
        $bulan = empty($search['bulan']) ? '' : $search['bulan'];
        //$tgl = $tahun . "-" . $bulan;
        $params = array($barang_noinventaris, $barang_nama, $tahun, $bulan);

        //sent to display
        $this->smarty->assign("search", $search);

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("inventarisasi/service/index/");
        $config['total_rows'] = $this->m_service->get_count_service_pagination($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */

        // get data
        $params = array($barang_noinventaris, $barang_nama, $tahun, $bulan, ($start - 1), $config['per_page']);
        $result = $this->m_service->get_all_service_pagination($params);
        $this->smarty->assign("rs_id", $result);


        //image 
        $filepath = base_url() . 'resource/userfile/services/';
        $this->smarty->assign("filepath", $filepath);
        
        // month
        $this->smarty->assign("bulan", $this->datetimemanipulation->get_month_indonesia());
        $this->smarty->assign("tahun", $this->m_service->get_year_service());

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    // proses pencarian
    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('search_service');
        } else {
            $params = array(
                "barang_noinventaris" => $this->input->post("barang_noinventaris"),
                "barang_nama" => $this->input->post("barang_nama"),
                "bulan" => $this->input->post("bulan"),
                "tahun" => $this->input->post("tahun")
            );
            $this->tsession->set_userdata("search_service", $params);
        }
        // redirect
        redirect("inventarisasi/service");
    }

    public function add() {
        // set page rule
        $this->_set_page_rule("C");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/service/add.html");

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add_process() {
        // set page rule
        $this->_set_page_rule("C");
        //cek data
        $this->tnotification->set_rules('barang_id', 'ID Barang', 'trim|required');
        $this->tnotification->set_rules('barang_data', 'Barang', 'trim');
        $this->tnotification->set_rules('services_desc', 'Deskripsi', 'trim|required');
        $this->tnotification->set_rules('services_tgl', 'Tanggal Mulai', 'trim|required');
        $this->tnotification->set_rules('services_tglselesai', 'Tanggal Selesai', 'trim|required');
        $this->tnotification->set_rules('services_parts', 'Biaya Spareparts', 'trim|required');
        $this->tnotification->set_rules('services_biaya', 'Biaya Service', 'trim|required');
        $this->tnotification->set_rules('services_status', 'Status', 'trim|required');
        $this->tnotification->set_rules('services_keterangan', 'Keterangan', 'trim');

        //proses
        if ($this->tnotification->run() !== FALSE) {
            $services_parts = $this->input->post('services_parts');
            $services_parts = str_replace(".", "", $services_parts);
            $services_biaya = $this->input->post('services_biaya');
            $services_biaya = str_replace(".", "", $services_biaya);
            // parameter 
            $params = array(
                'services_tgl' => $this->input->post('services_tgl'),
                'services_desc' => $this->input->post('services_desc'),
                'services_tglselesai' => $this->input->post('services_tglselesai'),
                'services_status' => $this->input->post('services_status'),
                'barang_id' => $this->input->post('barang_id'),
                'services_parts' => $services_parts,
                'services_biaya' => $services_biaya,
                'services_keterangan' => $this->input->post('services_keterangan'),
                'mdb' => $this->com_user['user_id']
            );
            // insert
            if ($this->m_service->insert($params)) {
                $last_services_id = $this->m_service->get_last_services_id();
                //upload
                if (!empty($_FILES['services_file']['tmp_name'])) {
                    // load
                    $this->load->library('tupload');
                    // delete 
                    $filename = $last_services_id;
                    $filepath = 'resource/userfile/services/' . $last_services_id;

                    // upload config
                    $config['upload_path'] = 'resource/userfile/services/';
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '10000';
                    $config['file_name'] = $filename;
                    $this->tupload->initialize($config);
                    // process upload images 
                    if (!$this->tupload->do_upload('services_file')) {
                        // jika gagal (kembalikan pesan)
                        $this->tnotification->set_error_message($this->tupload->display_errors());
                        $this->tnotification->sent_notification("error", "Foto gagal diupload");
                        $filename = null;
                    } else {
                        $data_upload = $this->tupload->data();
                        $filename = $data_upload['file_name'];
                        $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                        $param = array($data_upload['file_path'] . '' . $data_upload['file_name'], $last_services_id);
                        $this->m_service->update_file($param);
                    }
                }
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('inventarisasi/service/add');
    }
    
    public function edit($id) {
        $this->_set_page_rule("U");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/service/edit.html");

        //data
        $result = $this->m_service->get_detail_service_by_id($id);
        $this->smarty->assign("result", $result);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        $this->_set_page_rule("U");
        //cek data
        $this->tnotification->set_rules('services_id', 'ID Service', 'trim|required');
        $this->tnotification->set_rules('barang_id', 'ID Barang', 'trim|required');
        $this->tnotification->set_rules('barang_data', 'Barang', 'trim');
        $this->tnotification->set_rules('services_desc', 'Deskripsi', 'trim|required');
        $this->tnotification->set_rules('services_tgl', 'Tanggal Mulai', 'trim|required');
        $this->tnotification->set_rules('services_tglselesai', 'Tanggal Selesai', 'trim|required');
        $this->tnotification->set_rules('services_parts', 'Biaya Spareparts', 'trim|required');
        $this->tnotification->set_rules('services_biaya', 'Biaya Service', 'trim|required');
        $this->tnotification->set_rules('services_status', 'Status', 'trim|required');
        $this->tnotification->set_rules('services_keterangan', 'Keterangan', 'trim');
        $this->tnotification->set_rules('services_file', 'File', 'trim');

        //proses
        if ($this->tnotification->run() !== FALSE) {
            $services_parts = $this->input->post('services_parts');
            $services_parts = str_replace(".", "", $services_parts);
            $services_biaya = $this->input->post('services_biaya');
            $services_biaya = str_replace(".", "", $services_biaya);
            $last_services_id = $this->input->post('services_id');
            // parameter
            $params = array(
                'services_tgl' => $this->input->post('services_tgl'),
                'services_desc' => $this->input->post('services_desc'),
                'services_tglselesai' => $this->input->post('services_tglselesai'),
                'services_status' => $this->input->post('services_status'),
                'barang_id' => $this->input->post('barang_id'),
                'services_parts' => $services_parts,
                'services_biaya' => $services_biaya,
                'services_keterangan' => $this->input->post('services_keterangan'),
                'mdb' => $this->com_user['user_id'],
                'services_id' => $last_services_id
            );
 
            // update
            if ($this->m_service->update($params)) {  
                //upload
                if (!empty($_FILES['services_file']['tmp_name'])) {
                    // load
                    $this->load->library('tupload');
                    // delete 
                    $file = $this->input->post('services_file');
                        if (!empty($file)) {
                            if (is_file($file)) {
                                unlink($file);
                            }
                        }
                    $filename = $last_services_id;
                    $filepath = 'resource/userfile/services/' . $last_services_id;

                    // upload config
                    $config['upload_path'] = 'resource/userfile/services/';
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '10000';
                    $config['file_name'] = $filename;
                    $this->tupload->initialize($config);
                    // process upload images 
                    if (!$this->tupload->do_upload('services_file')) {
                        // jika gagal (kembalikan pesan)
                        $this->tnotification->set_error_message($this->tupload->display_errors());
                        $this->tnotification->sent_notification("error", "File gagal diupload");
                        $filename = null;
                    } else {
                        $data_upload = $this->tupload->data();
                        $filename = $data_upload['file_name'];
                        $this->tnotification->sent_notification("success", "File berhasil disimpan");
                        $param = array($data_upload['file_path'] . '' . $data_upload['file_name'], $last_services_id);
                        $this->m_service->update_file($param);
                    }
                }
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } 
        redirect('inventarisasi/service/edit/' . $this->input->post('services_id'));
    }

    public function delete($id) {
        $this->_set_page_rule("D");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/service/delete.html");

        //data
        $result = $this->m_service->get_detail_service_by_id($id);
        $this->smarty->assign("result", $result);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        $this->_set_page_rule("D");
        //cek data
        $this->tnotification->set_rules('services_id', 'ID Services', 'trim|required');
        $this->tnotification->set_rules('services_file', 'File', 'trim');

        //proses
        if ($this->tnotification->run() !== FALSE) {
            // delete
            if ($this->m_service->delete($this->input->post('services_id'))) {
                // file
                $filepath = $this->input->post('services_file');
                if (!empty($filepath)) {
                    if (is_file($filepath)) {
                        unlink($filepath);
                    }
                }
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data service berhasil dihapus");
                 redirect('inventarisasi/service');
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data service gagal dihapus");
            }
        }
        redirect('inventarisasi/service/delete/'.$this->input->post('services_id'));
    }

    // dialog pencarian barang
    public function cari_barang() {
        // parameter
        $barang_noinventaris = $this->input->post('barang_noinventaris');
        $noinventaris = (empty($barang_noinventaris)) ? '%%' : "%" . $barang_noinventaris . "%";
        $barang_nama = $this->input->post('barang_nama');
        $nama = (empty($barang_nama)) ? '%%' : "%" . $barang_nama . "%";
        // get dasar hukum by number
        if ((!empty($noinventaris)) || (!empty($nama))) {
            $html = "";
            //paging
            //jumlah data
            $total = $this->m_barang->count_barang_by_noinventaris_nama(array($noinventaris, $nama));
            /* start of pagination --------------------- */
            //offset
            $offset = $this->input->post('offset');
            $offset = empty($offset) ? 0 : intval($offset);
            // pagination
            $config['base_url'] = '';
            $config['total_rows'] = $total;
            $config['per_page'] = 20;
            $config['uri_segment'] = ($offset / $config['per_page']) + 1;
            $this->pagination->initialize($config);
            $pagination['data'] = $this->pagination->create_links_dialog_box($offset);
            // pagination attribute
            $start = $offset + 1;
            $end = $offset + $config['per_page'];
            $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
            $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
            $pagination['end'] = $end;
            $pagination['total'] = $config['total_rows'];
            /* end of pagination ---------------------- */
            //get data
            $rs_id = $this->m_barang->get_barang_by_noinventaris_nama(array($noinventaris, $nama, ($start - 1), $config['per_page']));
            if (!empty($rs_id)) {
                $html .= '<div class="navigation">
                            <div class="pageRow">
                                <div class="pageNav">
                                    <ul>
                                        <li class="info"><strong>' . $pagination['start'] . ' - ' . $pagination['end'] . '</strong> dari <strong>' . $pagination['total'] . '</strong> Data</li>
                                        ' . $pagination['data'] . '
                                    </ul>
                                </div>
                            <div class="clear"></div>
                            </div>
                        </div>';
                $html .= '<table id="table-view" class="table-view" width="100%">
                                <tr>
                                    <th width="5%">No</th>
                                    <th width="30%">No Inventaris</th>
                                    <th width="45%">Nama Barang</th>
                                    <th width="15%"></th>
                                </tr>';
                $no = $start; //$offset + 1;
                foreach ($rs_id as $rec) {
                    $row_style = ($no % 2) == 0 ? 'class="blink-row"' : '';
                    $url_link = '<a href="#" class="button-edit" title="' . $rec['barang_noinventaris'] . ' | ' . $rec['barang_nama'] . '" lang="' . $rec['barang_id'] . '" >Pilih</a>';
                    // parse data
                    $html .= '<tr ' . $row_style . '>
                                <td align="center">' . $no . '.</td>
                                <td align="center"><small>' . $rec['barang_noinventaris'] . '</small></td>
                                <td><small>' . $rec['barang_nama'] . '</small></td>
                                <td align="center">' . $url_link . '</td>
                                </tr>';
                    $no++;
                }
                $html .= '</table>';
                // print out result
                echo $html;
            } else {
                echo '<p style="font-size: 11px; color: #666;">Data barang tidak ditemukan, hubungi administrator untuk menambah data barang!</p>';
            }
        } else {
            echo '<p style="font-size: 11px; color: #666;">Masukkan nomor inventaris dan atau nama barang diatas!</p>';
        }
    }

    public function view_service($id) {
        $rs_service = $this->m_service->get_detail_service_by_id($id);

        $html = '<tbody>
                <tr>
                    <th width="25%">No. Inventaris</th>
                    <td width="75%">' . $rs_service['barang_noinventaris'] . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Nama Barang</th>
                    <td width="75%">' . $rs_service['barang_nama'] . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Deskripsi</th>
                    <td width="75%">' . $rs_service['services_desc'] . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Tgl. Mulai</th>
                    <td width="75%">' . $this->datetimemanipulation->get_full_date($rs_service['services_tgl']) . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Tgl. Selesai</th>
                    <td width="75%">' . $this->datetimemanipulation->get_full_date($rs_service['services_tglselesai']) . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Biaya Spareparts</th>
                    <td width="75%">' . number_format($rs_service['services_parts'], 0, ",", ".") . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Biaya Service</th>
                    <td width="75%">' . number_format($rs_service['services_biaya'], 0, ",", ".") . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Keterangan</th>
                    <td width="75%">' . $rs_service['services_keterangan'] . '</td>  
                 </tr>
                 <tr>
                    <th width="25%">Status</th>
                    <td width="75%">' . ucwords(($rs_service['services_status'] == 'belum') ? $rs_service['services_status'] . ' selesai' : $rs_service['services_status']) . '</td>  
                 </tr>';
        $html .='</tbody>';

        echo $html;
    }

}

